﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace IosDevice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "ext files (*.txt)|*.txt|All files(*.*)|*>**";

            if (openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            if (!File.Exists(openFileDialog1.FileName))
            {
                MessageBox.Show("文件不存在");
                return;
            }

            List<DeviceInfo> deviceInfos = new List<DeviceInfo>();

            StreamReader st = new StreamReader(openFileDialog1.FileName, Encoding.Default);
            int index = 0;
            while (!st.EndOfStream)
            {
                index++;
              string  str = st.ReadLine().Replace(" ","");
                if (str.Contains("|"))
                {
                   string[] keyValues =  str.Split('|');
                    if (keyValues.Length == 2)
                    {
                        DeviceInfo info = new DeviceInfo();
                        info.Key = keyValues[0];
                        info.Value = keyValues[1];
                        deviceInfos.Add(info);
                    }
                    
                }
                else if (str.Length == 40)
                {
                    DeviceInfo info = new DeviceInfo();
                    info.Key = "iphone index " + index;
                    info.Value =str;
                    deviceInfos.Add(info);
                }
            }


            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.AppendLine("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
            sb.AppendLine("<plist version=\"1.0\">");
            sb.AppendLine("<dict>");
            sb.AppendLine("<key>Device UDIDs</key>");
            sb.AppendLine("<array>");
            sb.AppendLine("</array>");
            sb.AppendLine("</dict>");
            sb.AppendLine("</plist>");

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(sb.ToString());
            XmlNode arrayXmlNode = xmldoc.SelectSingleNode("plist/dict/array");
            if (arrayXmlNode != null)
            {
                foreach (DeviceInfo item in deviceInfos)
                {
                    XmlElement dictXmlElement = xmldoc.CreateElement("dict");

                    XmlElement keyDevXmlElement = xmldoc.CreateElement("key");
                    keyDevXmlElement.InnerText = "deviceIdentifier";
                    XmlElement strDevXmlElement = xmldoc.CreateElement("string");
                    strDevXmlElement.InnerText = item.Value;

                    XmlElement keyNameXmlElement = xmldoc.CreateElement("key");
                    keyNameXmlElement.InnerText = "deviceName";
                    XmlElement strNameXmlElement = xmldoc.CreateElement("string");
                    strNameXmlElement.InnerText = item.Key;

                    dictXmlElement.AppendChild(keyDevXmlElement);
                    dictXmlElement.AppendChild(strDevXmlElement);
                    dictXmlElement.AppendChild(keyNameXmlElement);
                    dictXmlElement.AppendChild(strNameXmlElement);

                    arrayXmlNode.AppendChild(dictXmlElement);
                }
            }

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "files(*.deviceids)|*.deviceids";
            if (saveFileDialog1.ShowDialog()== DialogResult.OK)
            {
                xmldoc.Save(saveFileDialog1.FileName);
                return;
            }
            xmldoc.Save("iphone.deviceids");
        }
    }
}
